package model.pessoa;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.banco.ConexaoFactory;

public class SocioDAO {

	public void salvar(Socio socio) {
		Connection con = ConexaoFactory.getInstance().getConexao();

		String sql = "insert into clube.pessoa (nome, endereco, telefone, sexo, cpf, rg, data_nascimento, qtd_aula, qtd_evento, parcial, tipo_pessoa) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement statement = null;
		try {
			statement = con.prepareStatement(sql);
			statement.setString(1, socio.getNome());
			statement.setString(2, socio.getEndereco());
			statement.setString(3, socio.getTelefone());
			statement.setString(4, socio.getSexo().name());
			statement.setString(5, socio.getCPF());
			statement.setString(6, socio.getRG());
			statement.setString(7, socio.getDataNascimento());
			statement.setInt(8, socio.getQtdAula());
			statement.setInt(9, socio.getQtdEvento());
			statement.setBoolean(10, socio.isParcial());
			statement.setString(11, "socio");
			statement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public List<Socio> pesquisar(String nome) {
		List<Socio> socios = new ArrayList<Socio>();
		Socio socio = new Socio();
		String sql = "select * from clube.pessoa where nome like ? and tipo_pessoa like 'socio'";

		PreparedStatement statement = null;
		ResultSet result = null;

		try {
			statement = ConexaoFactory.getInstance().getConexao()
					.prepareStatement(sql);
			result = statement.executeQuery();

			while (result.next()) {
				((Pessoa) socios).setId(result.getLong("id"));
				((Pessoa) socios).setNome(result.getString("nome"));
				

				socios.add(socio);
			}
			result.close();
			statement.close();
			return socios;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}
}
